<template>
  <el-row class="login-page">
    <el-col :span="8">
      <el-form>
        <el-form-item>
          <h1>管理员登录</h1>
        </el-form-item>
        <el-form-item>
          <el-input
            :prefix-icon="User"
            v-model="user.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input type="password" :prefix-icon="Lock" v-model="user.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm" class="login-button">登录</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { request } from '@/utils/request'
import { useLoginStore } from '@/stores/loginStore';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage } from 'element-plus';
const router = useRouter();
const route = useRoute();
const loginStore = useLoginStore();
const user = ref({
  username: 'liarjo',
  password: '123456',
  remember: false,
})
const submitForm = async () => {
  try {
    const userForm = new FormData();
    userForm.append('username', user.value.username);
    userForm.append('password', user.value.password);
    const response = await request.post('/api/admin/login', userForm)
    loginStore.setLoginInfo(response);
    if (route.query.redirect) {
      router.push(route.query.redirect);
    }else{
      router.push('/admin/');
    }
  } catch (error) {
    ElMessage.success({
      message: error.message,
      duration: 2000
    });
  }
}
</script>

<style scoped lang="scss">
.login-page {
  height: 100%;
  justify-content: center;
  align-items: center;
  .login-button {
    width: 100%;
  }
}
</style>
